package com.example.lvsuo.service.impl;

import com.example.lvsuo.entity.vo.SystemAuthorityVo;
import com.example.lvsuo.service.ISystemAuthorityService;
import com.example.lvsuo.entity.SystemAuthority;
import com.example.lvsuo.mapper.SystemAuthorityMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统权限表 服务实现类
 * </p>
 *
 * @author 心心念念
 * @since 2021-03-30
 */
@Service
public class SystemAuthorityServiceImpl extends ServiceImpl<SystemAuthorityMapper, SystemAuthority> implements ISystemAuthorityService {

    @Autowired
    SystemAuthorityMapper sm;

    @Override
    public List<SystemAuthorityVo> querySa(String id) {
        List<SystemAuthorityVo>  rootMenu= sm.querySa(id);

        return prepareCascadeStream(rootMenu,0);
//        // 最后的结果
//        List<SystemAuthorityVo> menuList = new ArrayList<SystemAuthorityVo>();
//        // 先找到所有的一级菜单
//        for (SystemAuthorityVo systemAuthorityVo : rootMenu) {
//            // 一级菜单没有parentId
//            if (0 == systemAuthorityVo.getAuthorityPid()) {
//                menuList.add(systemAuthorityVo);
//            }
//        }
//        // 为一级菜单设置子菜单，getChild是递归调用的
//        for (SystemAuthorityVo menu : menuList) {
//            getChild(menu.getId(),menu, rootMenu);
//        }
//        Map<String,Object> jsonMap = new HashMap<>();
//        jsonMap.put("menu", menuList);
//        return jsonMap;
    }

    /**
     * 查询权限表-树形图
     * @return
     */
    @Override
    public List<SystemAuthorityVo> getSystemAuthority() {
//        1.获取所有菜单
        List<SystemAuthorityVo> list = sm.getList();
//        调用准备好的树形菜单数据
//        List<SystemAuthorityVo> childlen1 = getChildless(list, 0);
        List<SystemAuthorityVo> childlen2=getChildlessStream(list,0);

        return childlen2;
    }

    /**
     * 递归方法2
     * @param list
     * @param
     * @return
     */
    private List<SystemAuthorityVo> getChildlessStream(List<SystemAuthorityVo> list, Integer authorityPid) {
        /**
         * filter过滤,
         */
        //异步执行-开多个线程处理
        List<SystemAuthorityVo> collect=list.parallelStream().
                filter(a -> a.getAuthorityPid() == authorityPid)
                .map(a -> {
                    //查找子菜单
             List<SystemAuthorityVo> list1=getChildlessStream(list,a.getId());
//             放入菜单中
             a.setChildren(list1);
             return a;
                }).collect(Collectors.toList());//流转回集合
        return collect;
    }


//    /**
//     * 递归方法1
//     * 准备树形图数据
//     * @param list
//     * @param parentId
//     * @return
//     */
//  private List<SystemAuthorityVo> getChildless(List<SystemAuthorityVo> list, Integer parentId){
//      //返回所有的子菜单
//      List<SystemAuthorityVo> arrayList = new ArrayList();
////      2.查找所有一级菜单
//      for (int i=0;i<list.size();i++){
//          if (list.get(i).getAuthorityPid()==parentId){//父级菜单
//              arrayList.add(list.get(i));
//              List<SystemAuthorityVo> children = getChildless(list, list.get(i).getId());//子级菜单
//              list.get(i).setChildren(children);//设置子级菜单
//          }
//      }
////        3.查询二级菜单
//        return list;
//    }

     /** 获取 流程id
     * @param id
     * @return
     */
    @Override
    public String getSystemAuthorityByid(String id) {
        return sm.getSystemAuthorityByid(id);
    }

    /**
     * 修改模块FID
     * @param systemAuthority
     */
    @Override
    public void updateSystemAuthorityFid(SystemAuthority systemAuthority) {
            sm.updateSystemAuthorityFid(systemAuthority);
    }

    /**
     * 递归计算菜单
     * @param parentId
     * @param parentMenu
     * @param rootMenu
     */
    private void getChild(Integer parentId, SystemAuthorityVo parentMenu,List<SystemAuthorityVo> rootMenu) {
        // 子菜单
        List<SystemAuthorityVo> childList = new ArrayList<>();
        for (SystemAuthorityVo menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (0 != menu.getAuthorityPid()) {
                if (menu.getAuthorityPid().equals(parentId)) {
                    getChild(menu.getId(),menu, rootMenu);
                    childList.add(menu);
                }
            }
        }
        if (childList.size()>0) {
            parentMenu.setChildren(childList);
        }
    }


    private List<SystemAuthorityVo> prepareCascadeStream(List<SystemAuthorityVo> list, Integer parentid) {
        return list.parallelStream().filter(a -> a.getAuthorityPid() == parentid).map(a -> {
            //查找子菜单
            List<SystemAuthorityVo> menu = prepareCascadeStream(list, a.getId());
            //放入菜单中
            a.setChildren(menu);
            return a;
        }).collect(Collectors.toList());
    }
}
